package com.wisdytech.ccdserver.common;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author svolt
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * mysql配置
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.datasource.druid.db-type", havingValue = "mysql", matchIfMissing = true)
    public MybatisPlusInterceptor mysqlMybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * oracle配置
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.datasource.druid.db-type", havingValue = "oracle", matchIfMissing = false)
    public MybatisPlusInterceptor oracleMybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE_12C));
        return interceptor;
    }

    /**
     * sqlserver配置
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.datasource.druid.db-type", havingValue = "sqlserver", matchIfMissing = false)
    public MybatisPlusInterceptor sqlServerMybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER));
        return interceptor;
    }

    @Bean
    public DatabaseIdProvider getDatabaseIdProvider(){
        DatabaseIdProvider databaseIdProvider =new VendorDatabaseIdProvider();
        Properties properties =new Properties();
        properties.setProperty("Oracle","oracle");
        properties.setProperty("MySQL","mysql");
        properties.setProperty("SQL Server","sqlserver");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }
}
